/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ejb.model;

import ejb.article.ArticleBeanRemote;
import ejb.article.GroupBeanRemote;
import ejb.person.LanguageBeanRemote;
import ejb.person.SupplierBeanRemote;
import ejb.person.TelecomBeanRemote;
import ejb.person.TelecomtypeBeanRemote;
import ejb.person.address.AddressBeanRemote;
import ejb.person.address.CountryBeanRemote;
import gui.dialog.ProgressDialog;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.ejb.EJB;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

/**
 *
 * @author Gebruiker
 */
public class EJBModel {

    private static InitialContext context = null;
    @EJB
    private static LanguageBeanRemote lbr = null;
    @EJB
    private static CountryBeanRemote cbr = null;
    @EJB
    private static AddressBeanRemote abr = null;
    @EJB
    private static TelecomBeanRemote tbr = null;
    @EJB
    private static SupplierBeanRemote sbr = null;
    @EJB
    private static TelecomtypeBeanRemote ttbr = null;
    @EJB
    private static ArticleBeanRemote artbr = null;
    @EJB
    private static GroupBeanRemote agbr = null;

    public EJBModel() {
    }

    public static void initiate() {

        final ProgressDialog dialog = new ProgressDialog(new javax.swing.JFrame(), true, 6);


        Thread dialogThread = new Thread() {
            @Override
            public void run() {
                dialog.setVisible(true);
            }
        };
        dialogThread.start();
        dialog.setValue(0, "Connecting to server");
        setContext();
        dialog.setValue(1, "Creating languagebean");
        getLanguageBean();
        dialog.setValue(2, "Creating countrybean");
        getCountryBean();
        dialog.setValue(3, "Creating Supplierbean");
        getSupplierBean();
        dialog.setValue(4, "Creating Addressbean");
        getAddressBean();
        dialog.setValue(5, "Creating Telecombean");
        getTelecomBean();
        dialog.setValue(6, "Creating telecomtypebean");
        getTelecomtypeBean();
        getArticleBean();
        getArticleGroupBean();
        dialog.setVisible(false);
        
        



    }

    public static InitialContext setContext() {
        if (context == null) {
            try {
                Properties props = new Properties();
                props.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.enterprise.naming.SerialInitContextFactory");
                props.setProperty("org.omg.CORBA.ORBInitialHost", "localhost");
                // glassfish default port value will be 3700,
                props.setProperty("org.omg.CORBA.ORBInitialPort", "3700");
                context = new InitialContext(props);
            } catch (NamingException ex) {
                Logger.getLogger(EJBModel.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        return context;
    }

    public static LanguageBeanRemote getLanguageBean() {
        if (context == null) {
            setContext();
        }
        if (lbr == null) {
            try {
                lbr = (LanguageBeanRemote) context.lookup("ejb.person.LanguageBeanRemote");
            } catch (NamingException ex) {
                Logger.getLogger(EJBModel.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        return lbr;

    }

    public static CountryBeanRemote getCountryBean() {
        if (context == null) {
            setContext();
        }
        if (cbr == null) {
            try {
                cbr = (CountryBeanRemote) context.lookup("ejb.person.address.CountryBeanRemote");
            } catch (NamingException ex) {
                Logger.getLogger(EJBModel.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        return cbr;

    }

    public static SupplierBeanRemote getSupplierBean() {
        if (context == null) {
            setContext();
        }
        if (sbr == null) {
            try {
                sbr = (SupplierBeanRemote) context.lookup("ejb.person.SupplierBeanRemote");
            } catch (NamingException ex) {
                Logger.getLogger(EJBModel.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        return sbr;

    }

    public static AddressBeanRemote getAddressBean() {
        if (context == null) {
            setContext();
        }
        if (abr == null) {
            try {
                abr = (AddressBeanRemote) context.lookup("ejb.person.address.AddressBeanRemote");
            } catch (NamingException ex) {
                Logger.getLogger(EJBModel.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        return abr;

    }

    public static TelecomBeanRemote getTelecomBean() {
        if (context == null) {
            setContext();
        }
        if (tbr == null) {
            try {
                tbr = (TelecomBeanRemote) context.lookup("ejb.person.TelecomBeanRemote");
            } catch (NamingException ex) {
                Logger.getLogger(EJBModel.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        return tbr;

    }

    public static TelecomtypeBeanRemote getTelecomtypeBean() {
        if (context == null) {
            setContext();
        }
        if (ttbr == null) {
            try {
                ttbr = (TelecomtypeBeanRemote) context.lookup("ejb.person.TelecomtypeBeanRemote");
            } catch (NamingException ex) {
                Logger.getLogger(EJBModel.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        return ttbr;

    }
    
    public static ArticleBeanRemote getArticleBean() {
        if (context == null) {
            setContext();
        }
        if (artbr == null) {
            try {
                artbr = (ArticleBeanRemote) context.lookup("ejb.article.ArticleBeanRemote");
            } catch (NamingException ex) {
                Logger.getLogger(EJBModel.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        return artbr;

    }
    
    public static GroupBeanRemote getArticleGroupBean() {
        if (context == null) {
            setContext();
        }
        if (agbr == null) {
            try {
                agbr = (GroupBeanRemote) context.lookup("ejb.article.GroupBeanRemote");
            } catch (NamingException ex) {
                Logger.getLogger(EJBModel.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        return agbr;

    }
};
